package org.karelun.gef.parts;

import java.beans.PropertyChangeEvent;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.graphics.Image;
import org.karelun.gef.helper.ImageResource;
import org.karelun.gef.model.AttributeModel;
import org.karelun.gef.model.NodeConstants;

public class AttributePart extends BaseAttrPart {

	protected IFigure createFigure() {
		Label label = new Label();
		label.setIcon(getImage());
		return label;
	}

	private Image getImage() {
		return ImageResource.getImage(ImageResource.ATTRIBUTE);
	}

	@Override
	protected void refreshVisuals() {
		AttributeModel model=(AttributeModel)getModel();
		Label figure=(Label)getFigure();
		
		String text = model.getText();
		figure.setText(text);
		
		Point loc=model.getLocation();
		Dimension size = model.getSize();
		
		Rectangle rect = new Rectangle(loc,size);
		ClassPart parent = (ClassPart)getParent();
		parent.setLayoutConstraint(this, figure, rect);
		
		parent.refresh();
		super.refreshVisuals();
	}
	
	@Override
	public void activate() {
		super.activate();
		((AttributeModel)getModel()).addPropertyChangeListener(this);
	}
	
	@Override
	public void deactivate() {
		((AttributeModel)getModel()).removePropertyChangeListener(this);
		super.deactivate();
	}
	
	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if (PROP_ATTRUBUTE.equals(evt.getPropertyName()))
			refreshVisuals();
		else if(NodeConstants.PROP_TEXT.equals(evt.getPropertyName()))
			refreshVisuals();
	}
}